package com.example.mainstream.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.shopapp.R;
import com.example.shopapp.bean.User;
import com.example.shopapp.database.UserDBUtils;

public class UserAdapter extends BaseQuickAdapter<User, BaseViewHolder> {
    private Context context;
    private IUserListener listener;
    public UserAdapter(Context context, IUserListener listener) {
        super(R.layout.item_user);
        this.context = context;
        this.listener = listener;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, User user) {
        baseViewHolder.setText(R.id.tv_name, UserDBUtils.getInstance(context).getNameByUserId(user.getUser_id()));
        String path = UserDBUtils.getInstance(context).getHeadUrlBy(user.getUser_id());
        if (!TextUtils.isEmpty(path)){
            Glide.with(context).load(path).into((ImageView) baseViewHolder.getView(R.id.img_head));
        }else {
            Glide.with(context).load(R.drawable.head_default).into((ImageView) baseViewHolder.getView(R.id.img_head));
        }
        baseViewHolder.getView(R.id.card_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    listener.UserClick(user);
                }
            }
        });
    }
}
